/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poli.reserva.bean;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;


import poli.reserva.dao.JLoginMySQL;
import poli.reserva.bean.*;

/**
 *
 * @author DAVID MOTTA
 */
@ManagedBean
@SessionScoped

public class Login implements Serializable {
    private static final long serialVersionUID = 7765876811740798583L;
  
    private String cedula;
    
    private String nombre;
    
    private String apellido;
   
    private String email;
    
    private String pass;
    
    private String idusuario;
    
    private boolean loggedIn;
    
    private boolean createUser;
    
    @ManagedProperty(value="#{navigationBean}")
	private NavigationBean navigationBean;
    
    
  
    public Login() {
    }

    public Login(String cedula, String nombre, String apellido, String email, String pass, String idusuario) {
        this.cedula = cedula;
        this.nombre = nombre;
        this.apellido = apellido;
        this.email = email;
        this.pass = pass;
        this.idusuario = idusuario;
       
    }

    public Login(String email, String pass) {
        this.email = email;
        this.pass = pass;
    }
    

    

    public Login(String cedula) {
        this.cedula = cedula;
    }

    public String getCedula() {
        return cedula;
    }

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getIdusuario() {
        return idusuario;
    }

    public void setIdusuario(String idusuario) {
        this.idusuario = idusuario;
    }
    
    public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public void setNavigationBean(NavigationBean navigationBean) {
		this.navigationBean = navigationBean;
	}
    
    public String ingresoLogin(){
        
        JLoginMySQL users = new JLoginMySQL();
        List<Login> selectJusuario = users.selectJusuarioMySQL();
        
        for (int i = 0; i < selectJusuario.size(); i++) {
            Login login = selectJusuario.get(i);
            
            if(cedula.equalsIgnoreCase(login.getCedula())&& pass.equals(login.getPass())){
                
                                loggedIn = true;
				return navigationBean.redirectToBuscarvuelo();
            }
            
        }
        
        // Set login ERROR
		FacesMessage msg = new FacesMessage("Su cedula no existe a la base de datos!", "ERROR MSG");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
		
		// To to login page
		return navigationBean.toLogin();
    }
    
    
    public String doLogout() {
		// Set the paremeter indicating that user is logged in to false
		loggedIn = false;
		
		// Set logout message
		FacesMessage msg = new FacesMessage("Logout success!", "INFO MSG");
		msg.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, msg);
		
		return navigationBean.toLogin();
	}
    
      
   

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (cedula != null ? cedula.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Login)) {
            return false;
        }
        Login other = (Login) object;
        if ((this.cedula == null && other.cedula != null) || (this.cedula != null && !this.cedula.equals(other.cedula))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "[ cedula=" + cedula + " ]"+"[NOMBRE:]"+ this.email+"   [  pass : = ]"+this.pass;
    }
    
    
   
    
}
